package com.hippo.core.ui.compose.register

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.model.Country
import com.hippo.core.ui.components.BottomSheetDialog
import com.hippo.core.ui.components.ThemeButton

@Composable
fun RegionBottomSheet(
    selectedIndex: Int,
    countryList:List<Country>,
    onItemSelected: (Country) -> Unit,
    onDismissRequest: () -> Unit,
) {

    var checkedIndex by remember { mutableStateOf(selectedIndex) }

    BottomSheetDialog(
        title = stringResource(id = R.string.region_1),
        onDismissRequest = onDismissRequest
    ) {

        LazyColumn(modifier = Modifier.weight(1F)) {
            items(countryList.size) { index ->
                val item = countryList[index]
                RegionItem(
                    modifier = Modifier.fillMaxWidth(),
                    country = item,
                    isSelected = checkedIndex == index,
                ){
                    checkedIndex = index
                }

                HorizontalDivider(thickness = 0.5.dp)

            }
        }

        ThemeButton(
            text = stringResource(id = R.string.save),
            onClick = { onItemSelected(countryList[selectedIndex]) }
        )

    }

}

@Composable
fun RegionItem(
    modifier: Modifier,
    country: Country,
    isSelected: Boolean,
    onClick: () -> Unit
) {
    Row(
        modifier = modifier
            .padding(16.dp)
            .click {
            onClick()
        }
    ) {
        Text(
            modifier = modifier.weight(1F),
            text = country.englishName.orEmpty(),
            fontSize = 14.sp,
            color = if (isSelected) MaterialTheme.colorScheme.primary else androidx.compose.ui.graphics.Color.Black
        )

        Image(
            modifier = Modifier.width(24.dp),
            painter = if (isSelected) painterResource(id = R.mipmap.radio_s) else painterResource(id = R.mipmap.radio_n),
            contentDescription = null
        )
    }

}


@Preview
@Composable
private fun RegionBottomSheetPreview() {
    RegionBottomSheet(
        countryList = listOf(
            Country(countryId = "1", englishName = "United States"),
            Country(countryId = "2", englishName = "United Kingdom"),
            Country(countryId = "3", englishName = "Australia"),
            Country(countryId = "4", englishName = "Canada"),
            Country(countryId = "5", englishName = "India"),
        ),
        selectedIndex = 0,
        onItemSelected = {},
        onDismissRequest = {}
    )
}